/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package negocio;

import java.util.ArrayList;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import modelo.Sede;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;



public class SedeNegocio  {
    
    private static EntityManagerFactory emf = 
            Persistence.createEntityManagerFactory("Tranex");
    private static EntityManager em = emf.createEntityManager();

    public static void iniciarTransacao() {
        em.getTransaction().begin();
    }
    public static void finalizarTransacao() {
        em.getTransaction().commit();
    }
    public static void limpar() {
        em.clear();
    }
    public static void gravar(Sede sede) {
        
        em.persist(sede);
    }
    
    public static void atualiza(Sede sede) {
        
        em.merge(sede);
    }
    
    public static void excluir(Sede sede) {
        em.remove(sede);
    }
    
    public static Sede findByPk(Integer codSede) {
        return em.find(Sede.class, codSede);
    }
    
    public static ArrayList<Sede> findBy(Sede sede){
    	ArrayList<Sede> sedes = new ArrayList<Sede>();
    	Session session = HibernateUtil.getSessionFactory().openSession();
    	Example parametro = Example.create(sede)
    	.excludeZeroes() //exclude zero valued properties
    	.ignoreCase() //perform case insensitive string comparisons
    	.enableLike(MatchMode.ANYWHERE)//use like for string comparisons
    	; 

      	  Criteria criteria = session.createCriteria(sede.getClass()).add( parametro);
    	
    	  sedes = (ArrayList<Sede>) criteria.list();
    	return sedes;
    	
    }
    
    public static ArrayList<Sede> findAll(){
    	ArrayList<Sede> sedes = (ArrayList<Sede>) em.createQuery("from Sede sede ORDER BY sede.codSede").getResultList();
    	return sedes;
    	
    }
    
}
